#include "vtor_spi.h"

#ifdef __VTOR_SPI__

// 加入用户工程宏
#ifdef __DEMO_PROJECT__

#include "main.h"

// 加入需要依赖的变量
VtorSpi * w25qxxVtorSpi = {0};
VtorSpi* lcdSpi = {0}; // 1.8寸 128*160 RGB TFT LCD ST7735 NOMEN_TEC

// https://item.taobao.com/item.htm?_u=k2mp6q5sb32f&id=655384693398
// 黄保凯中景园0.96寸oled显示屏彩色OLED彩屏SSD1357 12864液晶屏
// https://pan.baidu.com/s/1XBNhjCJOMkzA03lPPilqkA   提取码：8888
// 35元 0.96英寸彩色oled模块 SSD1775Z 长29mm 高27mm
// gnd vcc scl sda res dc cs
VtorSpi* lcdSsd1357Spi = {0};

typedef struct
{
	VtorSpi* spi;
	GPIO_TypeDef* clkPort;
	uint16_t clkPin;
	GPIO_TypeDef* mosiPort;
	uint16_t mosiPin;
	GPIO_TypeDef* misoPort;
	uint16_t misoPin;
	GPIO_TypeDef* csPort;
	uint16_t csPin;
}VtorSpiMaster_Stm32HalArray;


VtorSpi vtorSpiMasterArray[3] = {0};

const static VtorSpiMaster_Stm32HalArray VtorSpiMasterGpioArray[] = 
{
	{&vtorSpiMasterArray[0],
		GPIOA, GPIO_PIN_5, GPIOA, GPIO_PIN_6, GPIOA, GPIO_PIN_7, GPIOA, GPIO_PIN_2},
	{&vtorSpiMasterArray[1],
		GPIOB, GPIO_PIN_3, GPIOB, GPIO_PIN_5, NULL, 	0, GPIOB, GPIO_PIN_6},
	{&vtorSpiMasterArray[2],
		GPIOB, GPIO_PIN_3, GPIOB, GPIO_PIN_5, GPIOB, GPIO_PIN_4, NULL, 0},
	
	{NULL, NULL, 0, NULL, 0},
};

int8_t VtorSpiMaster_FindSpiIndex(VtorSpi* spi)
{
	for(int8_t i = 0; VtorSpiMasterGpioArray[i].spi != NULL; i++)
	{
		if(spi == VtorSpiMasterGpioArray[i].spi)
		{
			return i;
		}
	}
	return -1;
}

void VtorSpi_WriteClk(VtorSpi* spi, uint8_t level)
{
	int8_t i = VtorSpiMaster_FindSpiIndex(spi);
	if(i >= 0)
	{
		HAL_GPIO_WritePin(VtorSpiMasterGpioArray[i].clkPort, VtorSpiMasterGpioArray[i].clkPin, (GPIO_PinState)level);
	}
}

void VtorSpi_WriteMosi(VtorSpi* spi, uint8_t level)
{
	int8_t i = VtorSpiMaster_FindSpiIndex(spi);
	if(i >= 0)
	{
		HAL_GPIO_WritePin(VtorSpiMasterGpioArray[i].mosiPort, VtorSpiMasterGpioArray[i].mosiPin, (GPIO_PinState)level);
	}
}

uint8_t VtorSpi_ReadMiso(VtorSpi* spi)
{
	int8_t i = VtorSpiMaster_FindSpiIndex(spi);
	if(i >= 0)
	{
		if(VtorSpiMasterGpioArray[i].misoPort != NULL)
		{
			return HAL_GPIO_ReadPin(VtorSpiMasterGpioArray[i].misoPort, VtorSpiMasterGpioArray[i].misoPin);
		}
	}
	return 0;
}

void VtorSpi_WriteCs(VtorSpi* spi, uint8_t level)
{
	int8_t i = VtorSpiMaster_FindSpiIndex(spi);
	if(i >= 0)
	{
		if(VtorSpiMasterGpioArray[i].csPort != NULL)
		{
			HAL_GPIO_WritePin(VtorSpiMasterGpioArray[i].csPort, VtorSpiMasterGpioArray[i].csPin, (GPIO_PinState)level);
		}
	}
}








void VtorSpi_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};

	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	
	
	// 根据不同实例进行细分配置
	for(int8_t i = 0; VtorSpiMasterGpioArray[i].spi != NULL; i++)
	{
		GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
		GPIO_InitStruct.Pin = VtorSpiMasterGpioArray[i].clkPin;
		HAL_GPIO_Init(VtorSpiMasterGpioArray[i].clkPort, &GPIO_InitStruct);
		GPIO_InitStruct.Pin = VtorSpiMasterGpioArray[i].mosiPin;
		HAL_GPIO_Init(VtorSpiMasterGpioArray[i].mosiPort, &GPIO_InitStruct);
		if(NULL != VtorSpiMasterGpioArray[i].csPort)
		{
			GPIO_InitStruct.Pin = VtorSpiMasterGpioArray[i].csPin;
			HAL_GPIO_Init(VtorSpiMasterGpioArray[i].csPort, &GPIO_InitStruct);
		}
		
		GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
		if(NULL != VtorSpiMasterGpioArray[i].misoPort)
		{
			GPIO_InitStruct.Pin = VtorSpiMasterGpioArray[i].misoPin;
			HAL_GPIO_Init(VtorSpiMasterGpioArray[i].misoPort, &GPIO_InitStruct);
		}
		
	}

	w25qxxVtorSpi = &vtorSpiMasterArray[0];
	w25qxxVtorSpi->cpol = SPI_POLARITY_HIGH; // clk空闲为高电平


	VtorSpi_WriteCs(w25qxxVtorSpi, GPIO_LEVEL_HIGH);
	VtorSpi_SetClkIdle(w25qxxVtorSpi);
	
	
	lcdSpi = &vtorSpiMasterArray[1];
	VtorSpi_WriteCs(lcdSpi, GPIO_LEVEL_HIGH);
	VtorSpi_SetClkIdle(lcdSpi);
	
	lcdSsd1357Spi = &vtorSpiMasterArray[2];
	lcdSsd1357Spi->cpol = SPI_POLARITY_LOW;
	VtorSpi_WriteCs(lcdSsd1357Spi, GPIO_LEVEL_HIGH);
	VtorSpi_SetClkIdle(lcdSsd1357Spi);
}


inline void VtorSpi_Delay(uint32_t tick)
{
	//HAL_Delay(tick);
	__nop();
	__nop();
	__nop();
}


#endif // __USER_PROJECT__

#endif // __VTOR_SPI__

